package com.tony.netty.server;

import com.tony.netty.PrintChannel;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @author xianghao
 * @date 2018/3/28 9:40
 */
public class ServerMain {

    public static void main(String[] args) throws InterruptedException {
        ServerBootstrap server = new ServerBootstrap();
        ChannelFuture channelFuture = server.group(new NioEventLoopGroup(1), new NioEventLoopGroup(1))
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 1024)
                .option(ChannelOption.SO_REUSEADDR, true)
                .handler(new ChannelInboundHandlerAdapter() {
                    @Override
                    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                        System.out.println("server-boss-channelRead");
                        PrintChannel.print(ctx.channel());
                        super.channelRead(ctx, msg);
                    }

                    @Override
                    public void channelActive(ChannelHandlerContext ctx) throws Exception {
                        System.out.println("server-boss-channelActive");
                        PrintChannel.print(ctx.channel());
                        super.channelActive(ctx);
                    }

                    @Override
                    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
                        System.out.println("server-boss-channelUnregistered");
                        PrintChannel.print(ctx.channel());
                        super.channelUnregistered(ctx);
                    }

                    @Override
                    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
                        System.out.println("server-boss-channelInactive");
                        PrintChannel.print(ctx.channel());
                        super.channelInactive(ctx);
                    }

                    @Override
                    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
                        System.out.println("server-boss-channelRegistered");
                        PrintChannel.print(ctx.channel());
                        super.channelRegistered(ctx);
                    }

                    @Override
                    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                        System.out.println("server-boss-exceptionCaught");
                        PrintChannel.print(ctx.channel());
                        cause.printStackTrace();
                        super.exceptionCaught(ctx, cause);
                    }

                    @Override
                    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
                        System.out.println("server-boss-userEventTriggered");
                        PrintChannel.print(ctx.channel());
                        System.out.println(evt);
                        super.userEventTriggered(ctx, evt);
                    }
                })
                .childHandler(new ChannelInitializer< SocketChannel>(){

                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast("childHandler",new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                System.out.println("server-child-channelRead:");
                                PrintChannel.print(ctx.channel());
                                super.channelRead(ctx, msg);
                            }

                            @Override
                            public void channelActive(ChannelHandlerContext ctx) throws Exception {
                                System.out.println("server-child-channelActive:");
                                PrintChannel.print(ctx.channel());
                                super.channelActive(ctx);
                            }

                            @Override
                            public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
                                System.out.println("server-child-channelUnregistered:");
                                PrintChannel.print(ctx.channel());
                                super.channelUnregistered(ctx);
                            }

                            @Override
                            public void channelInactive(ChannelHandlerContext ctx) throws Exception {
                                System.out.println("server-child-channelInactive:");
                                PrintChannel.print(ctx.channel());
                                super.channelInactive(ctx);
                            }

                            @Override
                            public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
                                System.out.println("server-child-channelRegistered:");
                                PrintChannel.print(ctx.channel());
                                super.channelRegistered(ctx);
                            }

                            @Override
                            public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                                System.out.println("server-child-exceptionCaught:");
                                PrintChannel.print(ctx.channel());
                                cause.printStackTrace();
//                                super.exceptionCaught(ctx, cause);
                            }

                            @Override
                            public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
                                System.out.println("server-child-userEventTriggered:");
                                PrintChannel.print(ctx.channel());
                                System.out.println(evt);
                                super.userEventTriggered(ctx, evt);
                            }
                        });
                    }
                })
                .bind(8800).sync();
    }
}
